// 预设学生数据（可根据需要扩展）
const studentData = {
    "2021001": { name: "张三", className: "计算机一班", major: "计算机科学与技术", year: "2021" },
    "2021002": { name: "李四", className: "计算机二班", major: "软件工程", year: "2021" },
    "2021003": { name: "王五", className: "电子一班", major: "电子信息工程", year: "2021" },
    "default": { name: "梁起越", className: "班级艺术设计1班", major: "艺术设计学", year: "2024" }
};

// DOM 加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 获取 DOM 元素
    const formSection = document.getElementById('formSection');
    const infoSection = document.getElementById('infoSection');
    const studentForm = document.getElementById('studentForm');
    const qrcodeModal = document.getElementById('qrcodeModal');
    const closeModal = document.getElementById('closeModal');

    // 表单提交逻辑
    studentForm.addEventListener('submit', function(e) {
        e.preventDefault(); // 阻止默认提交行为

        // 获取表单输入
        const name = document.getElementById('name').value.trim();
        const studentId = document.getElementById('studentId').value.trim();

        // 简单验证
        if (!name || !studentId) {
            alert('请填写完整的姓名和学号');
            return;
        }

        // 根据学号匹配预设数据
        const studentInfo = studentData[studentId] || studentData["default"];

        // 渲染学生信息
        document.getElementById('displayName').textContent = name;
        document.getElementById('displayStudentId').textContent = studentId;
        document.getElementById('displayClass').textContent = studentInfo.className;
        document.getElementById('displayMajor').textContent = studentInfo.major;
        document.getElementById('displayYear').textContent = studentInfo.year;

        // 切换显示区域（表单 → 信息）
        formSection.classList.add('hidden');
        infoSection.classList.remove('hidden');

        // 延迟显示二维码弹窗（增加过渡感）
        setTimeout(() => {
            qrcodeModal.classList.remove('hidden');
            setTimeout(() => {
                qrcodeModal.classList.add('opacity-100');
                qrcodeModal.querySelector('div').classList.remove('scale-95');
                qrcodeModal.querySelector('div').classList.add('scale-100');
            }, 10);
        }, 500);
    });

    // 关闭二维码弹窗
    closeModal.addEventListener('click', function() {
        qrcodeModal.classList.remove('opacity-100');
        qrcodeModal.querySelector('div').classList.remove('scale-100');
        qrcodeModal.querySelector('div').classList.add('scale-95');

        setTimeout(() => {
            qrcodeModal.classList.add('hidden');
        }, 300);
    });

    // 点击弹窗外部也可关闭
    qrcodeModal.addEventListener('click', function(e) {
        if (e.target === qrcodeModal) {
            closeModal.click();
        }
    });
});
